package com.sucisoft.base.tools;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AesUtils {

    /**
     * 加密
     *
     * @param encData   需要加密的内容
     * @param secretKey 加密密码
     * @param vector    IV
     * @return
     */
    public static String encrypt(String encData, String secretKey, String vector) {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            byte[] raw = secretKey.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            IvParameterSpec iv = new IvParameterSpec(vector.getBytes());// 使用CBC模式，需要一个向量iv，可增加加密算法的强度
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            return parseByte2HexStr(cipher.doFinal(encData.getBytes("utf-8")));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 解密
     *
     * @param encData   待解密内容
     * @param secretKey 解密密钥
     * @param vector    解密IV
     * @return
     */
    public static String decrypt(String encData, String secretKey, String vector) {
        try {
            byte[] raw = secretKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(vector.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] encrypted1 = parseHexStr2Byte(encData);
            byte[] original = cipher.doFinal(encrypted1);
            return new String(original, "utf-8");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将二进制转换成16进制
     *
     * @param buf
     * @return
     */
    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 将16进制转换为二进制
     *
     * @param hexStr
     * @return
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    static String nums = "[{\"id\":\"892d74bd7059479db2d7f14b0421b07f\",\"isNewRecord\":false,\"parentId\":\"08c8f7a6866946d1a1e51ca4fb411e15\",\"name\":\"技术报刊\",\"childList\":[{\"id\":\"85922831aaad4f0e9a3aa0d5b117ec46\",\"isNewRecord\":false,\"parentId\":\"892d74bd7059479db2d7f14b0421b07f\",\"name\":\"苹果专刊\",\"childList\":[{\"id\":\"7c0030c00eba493283915a5aac031c49\",\"isNewRecord\":false,\"parentId\":\"85922831aaad4f0e9a3aa0d5b117ec46\",\"name\":\"病虫草害\"},{\"id\":\"b34b73d4f94140b689832d84a12215a2\",\"isNewRecord\":false,\"parentId\":\"85922831aaad4f0e9a3aa0d5b117ec46\",\"name\":\"肥水管理\"},{\"id\":\"ca197749793f44b1a1131761daa954f3\",\"isNewRecord\":false,\"parentId\":\"85922831aaad4f0e9a3aa0d5b117ec46\",\"name\":\"栽培管理\"},{\"id\":\"6fcc72d1061849718328ec09301f2bdc\",\"isNewRecord\":false,\"parentId\":\"85922831aaad4f0e9a3aa0d5b117ec46\",\"name\":\"示范果园\"},{\"id\":\"0938be5927f847c2b88a12f25be1d2e4\",\"isNewRecord\":false,\"parentId\":\"85922831aaad4f0e9a3aa0d5b117ec46\",\"name\":\"派活单\"}]},{\"id\":\"2acf183f89e7439cb61754de8230f191\",\"isNewRecord\":false,\"parentId\":\"892d74bd7059479db2d7f14b0421b07f\",\"name\":\"桃树专刊\"},{\"id\":\"88c2105b8c6e42c3bec66d2a5d70c3c9\",\"isNewRecord\":false,\"parentId\":\"892d74bd7059479db2d7f14b0421b07f\",\"name\":\"核桃专刊\"},{\"id\":\"be9bcb6d096d4c53be0c06ffbaee5bfe\",\"isNewRecord\":false,\"parentId\":\"892d74bd7059479db2d7f14b0421b07f\",\"name\":\"杏树专刊\"},{\"id\":\"9505e7b248874e8abe98d771768b7fca\",\"isNewRecord\":false,\"parentId\":\"892d74bd7059479db2d7f14b0421b07f\",\"name\":\"枣树专刊\"},{\"id\":\"63d1bd3e18a64d2094cf473495106f72\",\"isNewRecord\":false,\"parentId\":\"892d74bd7059479db2d7f14b0421b07f\",\"name\":\"其他专刊\"}]},{\"id\":\"565e9d00e0d441c3bfdad8a178ae2e0a\",\"isNewRecord\":false,\"parentId\":\"08c8f7a6866946d1a1e51ca4fb411e15\",\"name\":\"技术视频\",\"childList\":[{\"id\":\"cc618d1f31b44d7481eb4ecb79b320f1\",\"isNewRecord\":false,\"parentId\":\"565e9d00e0d441c3bfdad8a178ae2e0a\",\"name\":\"大讲堂\"},{\"id\":\"b2dd8ec6c57a4bd2b21b279d0e3a5882\",\"isNewRecord\":false,\"parentId\":\"565e9d00e0d441c3bfdad8a178ae2e0a\",\"name\":\"跟我学\"},{\"id\":\"457e4d9e9f9648218ee492eb429701a9\",\"isNewRecord\":false,\"parentId\":\"565e9d00e0d441c3bfdad8a178ae2e0a\",\"name\":\"访谈录\"},{\"id\":\"8322c68abd40486a910aaeacc6db6600\",\"isNewRecord\":false,\"parentId\":\"565e9d00e0d441c3bfdad8a178ae2e0a\",\"name\":\"听讲座\"}]},{\"id\":\"59a99a670b344344a811f4d3ad33d6e3\",\"isNewRecord\":false,\"parentId\":\"08c8f7a6866946d1a1e51ca4fb411e15\",\"name\":\"最美果园\"},{\"id\":\"3bab22ea39b84a0ca888aa58a742c8e9\",\"isNewRecord\":false,\"parentId\":\"08c8f7a6866946d1a1e51ca4fb411e15\",\"name\":\"上榜专家\"}]";

    public static void main(String[] args) {

        if (nums.substring(0, 1).equals("[")) {
            nums = nums.substring(1, nums.length() - 1);
        }
        System.out.print(nums);

    }
}
